package com.baoyanghui.common;

import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.baoyanghui.model.ItemShortcut;
import com.baoyanghui.model.ItemType;
import com.baoyanghui.model.MaintainHistory;
import com.baoyanghui.model.Merchant;
import com.baoyanghui.model.MerchantPlace;

public class DataChanger {
	public static Merchant getMerchant(JSONObject data) {
		Merchant result = null;
		JSONObject tmp = null;
		JSONArray tmp2 = null;
		List<String> photos = new ArrayList<String>();
		
		try {
			tmp = data.getJSONObject("resultData");
			if (tmp != null && tmp.containsKey("userid")) {
				result = new Merchant();
				
				result.setAuthenticationFlg(tmp.containsKey("authenticationFlg") ? tmp.getString("authenticationFlg") : "");
				result.setBrandName(tmp.containsKey("brandName") ? tmp.getString("brandName") : "");
				result.setCorporationName(tmp.containsKey("corporationName") ? tmp.getString("corporationName") : "");
				result.setEmail(tmp.containsKey("email") ? tmp.getString("email") : "");
				result.setIntroduction(tmp.containsKey("introduction") ? tmp.getString("introduction") : "");
				result.setMobile(tmp.containsKey("mobile") ? tmp.getString("mobile") : "");
				result.setOfficeTel(tmp.containsKey("officeTel") ? tmp.getString("officeTel") : "");
				result.setUserid(tmp.containsKey("userid") ? tmp.getString("userid") : "");
				
				if (tmp.containsKey("photos")) {
					tmp2 = tmp.getJSONArray("photos");
					for(int i = 0; tmp2 != null && i < tmp2.size(); i++) {
						photos.add(tmp2.getString(i));
					}
				}
				
				result.setPhotos(photos);
			}
		} catch (Exception e) {
			return result;
		}
		
		return result;
	}
	
	public static List<MerchantPlace> getPlaces(JSONObject data) {
		List<MerchantPlace> result = null;
		MerchantPlace place = null;
		JSONObject tmp = null;
		
		try {
			JSONArray jArray = data.getJSONObject("resultData").getJSONArray("items");

			if (jArray.size() > 0) {
				result = new ArrayList<MerchantPlace>();
				for (int i = 0; i < jArray.size(); i++) {
					tmp = jArray.getJSONObject(i);
					
					place = new MerchantPlace();
					place.setId(tmp.getString("id"));
					place.setAddress(tmp.getString("address"));
					place.setBusinessItem(tmp.getString("businessItem"));
					place.setGpsLocation(tmp.getString("gpsLocation"));
					place.setLinkMan(tmp.getString("linkMan"));
					place.setMobile(tmp.getString("mobile"));
					place.setOfficePhone(tmp.getString("officePhone"));
					place.setPlaceName(tmp.getString("placeName"));
					place.setStatus(tmp.getString("status"));
					place.setType(tmp.getString("type"));
					
					List<String> photoList = new ArrayList<String>();
					if (tmp.containsKey("placePhoto")) {
						JSONArray photos = tmp.getJSONArray("placePhoto");
						for (int j = 0; photos != null && j < photos.size(); j++) {
							photoList.add(photos.getString(j));
						}
					}
					
					place.setPlacePhoto(photoList);
					
					result.add(place);
				}
			}
		} catch (Exception e) {
			return result;
		}
		
		return result;
	}

	public static List<MaintainHistory> getMaintainCars(JSONObject data) {
		List<MaintainHistory> result = new ArrayList<MaintainHistory>();
		MaintainHistory carInfo = null;
		JSONObject tmp = null;
		
		try {
			JSONArray jArray = data.getJSONObject("resultData").getJSONArray("items");

			if (jArray.size() > 0) {
				for (int i = 0; i < jArray.size(); i++) {
					tmp = jArray.getJSONObject(i);
					
					carInfo = new MaintainHistory();
					carInfo.setId(tmp.containsKey("id") ? tmp.getString("id") : "");
					carInfo.setLicenseNumber(tmp.containsKey("licenseNumber") ? tmp.getString("licenseNumber") : "");
					carInfo.setCarModel(tmp.containsKey("carModel") ? tmp.getString("carModel") : "");
					carInfo.setCustomerName(tmp.containsKey("customerName") ? tmp.getString("customerName") : "");
					carInfo.setCustomerMobile(tmp.containsKey("customerMobile") ? tmp.getString("customerMobile") : "");
					carInfo.setMaintainDate(tmp.containsKey("maintainDate") ? tmp.getString("maintainDate") : "");
					carInfo.setStatus(tmp.containsKey("status") ? tmp.getString("status") : "");
					
					result.add(carInfo);
				}
			}
		} catch (Exception e) {
			return result;
		}
		
		return result;
	}
	
	public static List<ItemShortcut> getItemShortcuts(JSONObject data) {
		List<ItemShortcut> result = new ArrayList<ItemShortcut>();
		ItemShortcut shortcut = null;
		JSONObject tmp = null;
		
		try {
			JSONArray jArray = data.getJSONObject("resultData").getJSONArray("items");

			if (jArray.size() > 0) {
				for (int i = 0; i < jArray.size(); i++) {
					tmp = jArray.getJSONObject(i);
					
					shortcut = new ItemShortcut();
					
					shortcut.setShortcutId(tmp.containsKey("shortcutId") ? tmp.getString("shortcutId") : "");
					shortcut.setShortcutName(tmp.containsKey("shortcutName") ? tmp.getString("shortcutName") : "");
					shortcut.setItemId(tmp.containsKey("itemId") ? tmp.getString("itemId") : "");
					shortcut.setItemPhoto(tmp.containsKey("itemPhoto") ? tmp.getString("itemPhoto") : "");
					
					result.add(shortcut);
				}
			}
		} catch (Exception e) {
			return result;
		}
		
		return result;
	}
	
	/**
	 * 取得种类信息
	 */
	public static List<ItemType> getItemTypes(JSONObject data) {
		List<ItemType> result = new ArrayList<ItemType>();
		ItemType type = null;
		JSONObject tmp = null;
		
		try {
			JSONArray jArray = data.getJSONArray("resultData");

			if (jArray.size() > 0) {
				for (int i = 0; i < jArray.size(); i++) {
					tmp = jArray.getJSONObject(i);
					
					type = new ItemType();
					
					type.setTypeId(tmp.containsKey("typeId") ? tmp.getString("typeId") : "");
					type.setTypeName(tmp.containsKey("typeName") ? tmp.getString("typeName") : "");
					type.setIsLeaf(tmp.containsKey("isLeaf") ? tmp.getBoolean("isLeaf") : false);
					
					result.add(type);
				}
			}
		} catch (Exception e) {
			return result;
		}
		
		return result;
	}
}
